using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UIDesigner
{
  class SortedControls
  {
    public SortedControls(Form form)
    {
      m_controlNumber = 0;
      foreach (Control c in form.Controls)
      {
        m_controlNumber++;
      }
      var sortedControls = new Control[m_controlNumber];
      int i = 0;
      foreach (Control c in form.Controls)
      {
        sortedControls[i] = c;
        i++;
      }
      var list =
        from p in sortedControls
        orderby p.TabIndex
        select p;
      m_sortedControls = new Control[m_controlNumber];
      i = 0;
      foreach (Control c in list)
      {
        m_sortedControls[i] = c;
        i++;
      }
    }
    public bool Has<T>()
    {
      foreach (Control c in m_sortedControls)
      {
        if (c is T) return true;
      }
      return false;
    }
    public int ControlNumber
    {
      get
      {
        return m_controlNumber;
      }
    }
    public IEnumerator<Control> GetEnumerator()
    {
      for (int i = 0; i < m_controlNumber; i++)
      {
        yield return m_sortedControls[i];
      }
    }

    private int m_controlNumber;
    private Control[] m_sortedControls;
  }
}
